package com.dianrui.operation.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.dianrui.operation.R;
import com.dianrui.operation.util.MyUtil;
import com.dianrui.operation.util.ToastUtil;
import com.dianrui.operation.view.opertionzxing.activity.OpeationCaptureActivity;
import com.xiaoantech.sdk.XiaoanBleApiClient;


import butterknife.BindView;
import butterknife.OnClick;

/**
 * 盒子检测
 */
public class DeviceDetectionActivity extends BaseActivity {
    private String titles;


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.input_imel)
    EditText inputImel;

    private XiaoanBleApiClient apiClient;




    @Override
    public int getContentViewId() {
        return R.layout.activity_check_detection;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle=getIntent().getExtras();
            if (bundle != null) {
                titles=bundle.getString("title");
                title.setText(titles);
            }
        }
        XiaoanBleApiClient.Builder builder = new XiaoanBleApiClient.Builder(this);
        apiClient = builder.build();


        inputImel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 15) {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(inputImel.getWindowToken(), 0);
                    }
                }

            }
        });
    }

    @Override
    public void initData() {}


    @OnClick({R.id.back, R.id.select,R.id.scans})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.select:
                if (TextUtils.isEmpty(inputImel.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.input_car_number_incorrect1));
                    return;
                }
                if (!apiClient.isBluetoothAdapterOn()) {
                    ToastUtil.showToast(getString(R.string.please_open_blueteeth));
                    startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
                    return;
                }
                if (!MyUtil.isGpsEnable(DeviceDetectionActivity.this)) {
                    ToastUtil.showToast(getString(R.string.please_open_gps));
                }
                startSelect();
                break;

            case R.id.scans:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (ContextCompat.checkSelfPermission(DeviceDetectionActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                        Intent intent=new Intent(DeviceDetectionActivity.this, OpeationCaptureActivity.class);
                        intent.putExtra("camflag","6");
                        startActivityForResult(intent,106);
                    } else {
                        ActivityCompat.requestPermissions(DeviceDetectionActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
                    }
                } else {
                    Intent intent=new Intent(DeviceDetectionActivity.this, OpeationCaptureActivity.class);
                    intent.putExtra("camflag","6");
                    startActivityForResult(intent,106);
                }
                break;

            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==106){
            String imel=data.getStringExtra("IMEI");
            inputImel.setText(imel);

        }
    }


    //跳转并发指令
    private void startSelect() {
        startActivity(new Intent(DeviceDetectionActivity.this,XiaoAnCommandActivity.class).putExtra("number",inputImel.getText().toString().trim()));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Intent intent=new Intent(DeviceDetectionActivity.this, OpeationCaptureActivity.class);
                    intent.putExtra("camflag","6");
                    startActivityForResult(intent,106);
                }
                break;

                default:
                    break;
        }

    }
}
